﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TireService.Data
{
    /// <summary>
    /// Validation rules for Customer
    /// </summary>
    public partial class Customer
    {
        TireServiceDB db = new TireServiceDB();

        /// <summary>
        /// Validation rules to ensure data is filled out in mandatory fields
        /// </summary>
        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (this.Name == null | this.Name == string.Empty)
            {
                throw new ArgumentException("Navn skal udfyldes.");
            }

            if (this.Address == null | this.Address == string.Empty)
            {
                throw new ArgumentException("Adresse skal udfyldes.");
            }

            if (this.ZipCode == null | this.ZipCode == string.Empty)
            {
                throw new ArgumentException("Postnr skal udfyldes.");
            }

            if (this.City == null | this.City == string.Empty)
            {
                throw new ArgumentException("By skal udfyldes.");
            }

            if (this.CountryId == null)
            {
                throw new ArgumentException("Land skal udfyldes.");
            }

            if (this.Phone1 == null | this.Phone1 == string.Empty)
            {
                throw new ArgumentException("Telefon skal udfyldes.");
            }

            if (this.Phone1Type == null)
            {
                throw new ArgumentException("Type for telefon 1 skal udfyldes.");
            }

            if (this.Email == null | this.Email == string.Empty)
            {
                throw new ArgumentException("Email skal udfyldes.");
            }
        }

        /// <summary>
        /// Validation rule to enforce email is in valid format
        /// </summary>
        /// <param name="value">value to validate</param>
        partial void OnEmailChanging(string value)
        {
            Regex email = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            if (email.IsMatch(value) == false)
            {
                throw new ArgumentException("Email er ikke gyldig.");
            }
        }

    }
}
